//
//  XLHomeCell.m
//  industry
//
//  Created by xl on 2020/11/25.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLHomeCell.h"
#import "XLAppletModel.h"

@interface XLHomeCell ()

@property (nonatomic, strong) XLView *bgView;
@property (nonatomic, strong) XLImageView *iconIG;
@property (nonatomic, strong) XLLabel *titleLB;

@end

@implementation XLHomeCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.bgView];
        [_bgView addSubview:self.iconIG];
        [self.contentView addSubview:self.titleLB];
        
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(52);
        }];
        
        [_iconIG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(self.bgView);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        
        [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.top.equalTo(self.bgView.mas_bottom).offset(5);
        }];
    }
    return self;
}

#pragma mark - Public
- (void)loadCellData:(XLAppletModel *)model {
    if (model.isMore) {
        UIImage *image = XLIMAGE(@"hmore");
        CGSize size = image.size;
        if (size.width > 52) {
            size = CGSizeMake(52, 52);
        }
        _iconIG.image = image;
        [_iconIG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(size);
        }];
    } else {
        [_iconIG sd_setImageWithURL:XLWebIMAGEURL(model.appletIcon) completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            CGSize size = image.size;
            if (size.width > 52) {
                size = CGSizeMake(52, 52);
            }
            self->_iconIG.image = image;
            [self->_iconIG mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(size);
            }];
        }];
    }
    _titleLB.text = model.appletName;
}

#pragma mark - setter/getter
- (XLImageView *)iconIG {
    if (!_iconIG) {
        _iconIG = [XLImageView new];
    }
    return _iconIG;
}

- (XLView *)bgView {
    if (!_bgView) {
        _bgView = [XLView new];
        _bgView.backgroundColor = [UIColor grayBgColor];
        _bgView.layer.cornerRadius = 12.f;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (XLLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [XLLabel new];
        _titleLB.font = [UIFont fontOfPingFangSCRegularWithSize:12];
        _titleLB.textColor = [UIColor blackFontColor];
    }
    return _titleLB;
}
@end
